#include "QualificationRound_RN.h"
#include <fstream>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <iterator>
using namespace std;

QualificationRound_RN::QualificationRound_RN(void)
{
}


QualificationRound_RN::~QualificationRound_RN(void)
{
}


bool QualificationRound_RN::IsRecycled(int number, int upperBound)
{
	string s = to_string((_Longlong)number);
	for(int i=1; i<s.length(); i++)
	{
		s = Recycle(s);
		int recycled = atoi(s.c_str());
		if(recycled > number && recycled <= upperBound)
		{
			return true;
		}
	}

	return false;
}

string QualificationRound_RN::Recycle(string s)
{
	string result;
	for(int i=1; i< s.length(); i++)
	{
		result += s[i];
	}
	result += s[0];
	return result;
}

void QualificationRound_RN::RecycledNumbers(const char* inputFile, const char* outputFile)
{
	string line;
	ifstream myfile(inputFile);
	ofstream output;
	output.open (outputFile);

	if(myfile.is_open())
	{		
		getline(myfile, line);
		int numberOfInputs = atoi(line.c_str());
		for(int i=1; i<= numberOfInputs; i++)
		{
			getline(myfile, line);
			istringstream iss(line);
			vector<string> tokens;
			copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter<vector<string>>(tokens));

			output << "Case #" << i << ": " <<  NumberOfRecycled(atoi(tokens[0].c_str()), atoi(tokens[1].c_str())) << endl;
		}

		myfile.close();
	}	
	output.close();
}


int QualificationRound_RN::NumberOfRecycled(int lowerBound, int upperBound)
{
	int result = 0;
	for(int i=lowerBound; i< upperBound; i++)
	{
		if(IsRecycled(i, upperBound))
			result++;
	}

	return result;
}